/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 业务员管理页面接口
 */
import request from '@/utils/request';
import { isArray } from 'lodash';
import { message } from 'antd';
import type { ResponseReturned } from '@/utils/request';
import type { SearchParams, OptionsParams } from '@/pages/OperatorManagement/types/WsPartner';
import type {
  AdvTableListItem,
  AdvSearchParams,
} from '@/pages/OperatorManagement/types/Advertisements';
import { CashBackTableRes } from '@/pages/OperatorManagement/types/CashBack';
import { checkAndTriggerRefresh } from '@/components/_pages/red-point/_utils';

// 业务员管理页面接口，可疑驿站地址审核，获取列表
export async function getTableList(params: any, fromRedDot?: boolean) {
  return request('/Inn/interceptUsers', {
    method: 'POST',
    data: params,
  })
    .then(({ data = {} }: { data: { data?: any[]; page?: number; total?: number } }) => {
      const { data: tableList = [], page, total } = data || {};

      if (isArray(tableList) && tableList.length > 0) {
        return {
          page,
          total,
          success: true,
          data: tableList,
        };
      }
      return {};
    })
    .then(checkAndTriggerRefresh(getTableList, fromRedDot));
}

// 业务员管理页面接口，可疑驿站地址审核，获取详细信息
export async function getYzInfo(params: any) {
  return request('/Inn/getApplicationInfo', {
    method: 'POST',
    data: params,
  });
}

// 业务员管理页面接口，拉黑或者取消拉黑
export async function operation(params: any) {
  return request('/Inn/updateInterceptUser ', {
    data: params,
    method: 'POST',
  });
}

// 微商合伙人
export async function getWsPartnerList(
  params: SearchParams,
  cb?: (list: any[]) => void,
): Promise<any> {
  return request('/KdyInviteVhomeRewards/list', {
    method: 'POST',
    data: params,
  }).then(({ data }) => {
    const { page_size } = params;
    const { list: dataList, count, page } = data || {};
    let list = dataList;
    if (isArray(list) && list.length > 0) {
      list = list.map((val, index) => ({
        ...val,
        num: (Number(page) - 1) * Number(page_size) + index + 1,
      }));
      if (cb) {
        cb(list);
      }
      return {
        total: count,
        success: true,
        data: list,
        current: page,
      };
    }
    return {};
  });
}

export async function changeWsPartnerStatus(params: OptionsParams): Promise<any> {
  return request('/KdyInviteVhomeRewards/set', {
    method: 'POST',
    data: params,
  });
}

export async function getPlatForm(params: any): Promise<any> {
  return request('/KdyInviteVhomeRewards/thirdMarkList', {
    method: 'POST',
    data: params,
  });
}

// 面单广告位获取广告列表
export async function getAdvList(
  params: AdvSearchParams,
  cb?: (list: any[]) => void,
): Promise<any> {
  return request('/CloudPrintAd/list', {
    method: 'POST',
    data: params,
  }).then(({ data }) => {
    const { list, total, page }: { list: AdvTableListItem[]; total: string; page: string } =
      data || {};
    if (isArray(list) && list.length > 0) {
      if (cb) {
        cb(list);
      }
      return {
        total,
        success: true,
        data: list,
        current: page,
      };
    }
    return {};
  });
}

// 面单广告，删除广告
export const deleteAdv: (params: { id: string }) => Promise<any> = async (params) => {
  return request('/CloudPrintAd/remove', {
    method: 'POST',
    data: params,
  });
};

// 面单广告，广告上下线
export const online: (params: { status: string; id: string }) => Promise<any> = async (params) => {
  return request('/CloudPrintAd/status', {
    method: 'POST',
    data: params,
  });
};

// 面单广告，上传图片
export const uploadImg: (params: { file: any }) => Promise<any> = async (params) => {
  return request('/CloudPrintAd/upload', {
    method: 'POST',
    data: params,
  });
};

// 面单广告，发布广告
export const createAdv: (params: any) => Promise<any> = async (params) => {
  return request('/CloudPrintAd/create', {
    method: 'POST',
    data: params,
  });
};
// 面单广告，发布广告
export const copyAdv: (params: { id: string }) => Promise<any> = async (params) => {
  return request('/CloudPrintAd/copy', {
    method: 'POST',
    data: params,
  });
};
// 面单广告，编辑广告
export const editAdv: (params: any) => Promise<any> = async (params) => {
  return request('/CloudPrintAd/modify', {
    method: 'POST',
    data: params,
  });
};
// 面单广告，预览广告
export const previewAdv: (params: any) => Promise<any> = async (params) => {
  return request('/CloudPrintAd/preview', {
    method: 'POST',
    data: params,
  });
};
// 面单广告，广告日志
export const getAdvLogList: (params: any) => Promise<any> = async (params) => {
  return request('/CloudPrintAd/logs', {
    method: 'GET',
    params,
  }).then(({ data }) => {
    const { list, total, page }: { list: AdvTableListItem[]; total: string; page: string } =
      data || {};
    if (isArray(list) && list.length > 0) {
      return {
        total,
        success: true,
        data: list,
        current: page,
      };
    }
    return {};
  });
};

// 添加&编辑面单控量广告
export async function createControlAdv(params: any): Promise<any> {
  return request('/CloudPrintAdPlan/compilePlan', {
    method: 'POST',
    data: params,
  });
}
// 面单控量广告位获取广告列表
export async function getControlAdvList(
  params: AdvSearchParams,
  cb?: (data: any) => void,
): Promise<any> {
  return request('/CloudPrintAdPlan/showPlan', {
    method: 'POST',
    data: params,
  }).then(({ data }) => {
    if (isArray(data) && data.length > 0) {
      if (cb) {
        cb(data);
      }
      return {
        success: true,
        data,
      };
    }
    return {};
  });
}
// 面单控量广告，发布广告
export const copyControlAdv: (params: { plan_id: string }) => Promise<any> = async (params) => {
  return request('/CloudPrintAdPlan/copyPlan', {
    method: 'POST',
    data: params,
  });
};

// 面单控量广告，广告上下线 1上线 2下线 3删除
export const onlineOrDelete: (params: { plan_status: string; plan_id: string }) => Promise<any> =
  async (params) => {
    return request('/CloudPrintAdPlan/updateStatus', {
      method: 'POST',
      data: params,
    });
  };
// 获取控量选择广告列表
export const offlineAdInfo: (params: any) => Promise<any> = async (params) => {
  return request('/CloudPrintAdPlan/offlineAdInfo', {
    method: 'POST',
    data: params,
  });
};

// 驿站代收开通审核
export async function getApplicationList(params: any) {
  return request('/Brand/getApplicationList', {
    method: 'POST',
    data: params,
  });
}

// 业务员管理页面接口，可疑驿站地址审核，获取详细信息
export async function changeInfoDetailed(params: any) {
  return request('/Brand/getApplicationInfo', {
    method: 'POST',
    data: params,
  });
}

// 业务员管理页面接口，可疑驿站地址审核，获取详细信息
export async function getYzLogList(params: any) {
  return request('/OperateLog/getList', {
    method: 'POST',
    data: params,
  });
}

// 业务员管理页面接口，可疑驿站地址审核，驳回接口
export async function changeInfoReject(params: any) {
  return request('/Brand/processApplication', {
    method: 'POST',
    data: params,
  });
}

// 获取设备拉黑列表
export async function searchPhoneDevice(params: any) {
  return request('/BlackPullingDeviceManage/searchPhoneDevice', {
    method: 'POST',
    data: params,
  });
}
// 获取设备拉黑日志
export async function getPullingBlackLog(params: any) {
  return request('/BlackPullingDeviceManage/getPullingBlackLog', {
    method: 'POST',
    data: params,
  });
}

// 设置设备拉黑
export async function operatePullingBlack(params: any) {
  return request('/BlackPullingDeviceManage/operatePullingBlack', {
    method: 'POST',
    data: params,
  });
}

export async function getYjOrder(params: any) {
  // /wkd/WkdOrderMonitor/lists
  return request('/wkd/WkdOrderMonitor/lists', {
    method: 'POST',
    data: params,
  }).then(({ data: res, code, msg }) => {
    const { data, total } = res;
    if (code != 0) {
      return Promise.reject({ success: false, msg });
    }
    if (isArray(data) && data.length > 0) {
      return {
        success: true,
        data,
        total,
      };
    }
    return {};
  });
}

export async function getQueryConfig() {
  return request('/wkd/WkdOrderMonitor/getQueryConfig', {
    method: 'POST',
  }).then(({ data, code }) => {
    return {
      success: code === 0 && data ? true : false,
      data,
    };
  });
}
export async function getLogisticsTrack(params: any) {
  return request('/wkd/WkdOrderMonitor/getLogisticsTrack', {
    method: 'POST',
    data: params,
  }).then(({ data, code }) => {
    return {
      success: code === 0 && isArray(data) && data.length > 0,
      data,
    };
  });
}

export async function getMonitorConfig(params: any) {
  return request('/wkd/WkdOrderMonitor/getMonitorConfig', {
    method: 'POST',
    data: params,
  });
}

export async function setMonitorConfig(params: any) {
  return request('/wkd/WkdOrderMonitor/setMonitorConfig', {
    method: 'POST',
    data: params,
  });
}
export async function getBrandList() {
  return request('/wkd/Blacklist/getBrandList', {
    method: 'POST',
  }).then((res) => {
    const { code, data } = res;
    return {
      success: code == 0,
      data,
    };
  });
}

export async function addDownloadTask(params: any) {
  return request('/wkd/WkdOrderMonitor/createExportTask', {
    method: 'POST',
    data: { ...params },
  }).then((res) => {
    const { code, data, msg } = res;
    return {
      success: code == 0,
      data,
      msg,
    };
  });
}

export async function getDownloadTaskList() {
  return request('/wkd/WkdOrderMonitor/exportTaskLists', {
    method: 'POST',
  }).then((res) => {
    const { code, data = {} } = res;

    return {
      success: code == 0,
      data: data.data,
    };
  });
}

export async function orderCancel(params: any) {
  return request('/order/cancel', {
    method: 'POST',
    data: { ...params },
  }).then((res) => {
    const { code, msg } = res;
    return {
      success: code == 0,
      msg,
    };
  });
}

/** 驿站经营信息审核列表 */
export async function getInfoVerifyList(params: YzInfoVerify.ReqParams, fromRedDot?: boolean) {
  return request<YzInfoVerify.Res>('/InnAudit/innAuditList', {
    method: 'POST',
    data: params,
  })
    .then(({ code, data, msg }) => {
      if (code == 0) {
        const { audits: list, page, count: total } = data || {};
        if (isArray(list)) {
          return {
            total,
            success: true,
            data: list,
            current: page,
          };
        }
      } else {
        message.error(msg);
      }
      return {
        total: 0,
        success: false,
        data: [],
        current: 1,
      };
    })
    .then(checkAndTriggerRefresh(getInfoVerifyList, fromRedDot));
}

/** 驿站经营信息详情 */
export async function getInfoVerifyDetail(params: { id: string }) {
  return request<YzInfoVerify.DetailRes>('/InnAudit/innAuditDetailt', {
    method: 'POST',
    data: params,
  });
}

/** 驿站经营信息详情，审核通过或拒绝 */
export async function passOrReject(params: { cm_id: string; type: string; status: string }) {
  return request<ResponseReturned>('/InnAudit/actSaveDetail', {
    method: 'POST',
    data: params,
  });
}
/** 驿站经营信息详情，审核通过或拒绝 */
export async function getNext(params: { id: string }) {
  return request<ResponseReturned>('/InnAudit/innNextDetail', {
    method: 'POST',
    data: params,
  });
}

/** 入库返现列表 */
export async function getCashBackList(params: any) {
  return request<CashBackTableRes>('/Inn/getCashBackList', {
    method: 'POST',
    data: params,
  }).then(({ code, data, msg }) => {
    if (code == 0) {
      const { list, page, total } = data || {};
      if (isArray(list) && list.length > 0) {
        return {
          total,
          success: true,
          data: list,
          current: page,
        };
      }
    } else {
      message.error(msg);
    }
    return {
      total: 0,
      success: false,
      data: [],
      current: 1,
    };
  });
}
/** 入库返现列表，发放 */
export async function cashBackDispatch(params: { batch: string }) {
  return request<ResponseReturned>('/Inn/releaseCash', {
    method: 'POST',
    data: params,
  });
}
/** 入库返现列表，添加 */
export async function cashBackAdd(params: { phone: string; money: number }) {
  return request<ResponseReturned>('/Inn/updateCashBackInfo', {
    method: 'POST',
    data: params,
  });
}
/** 入库返现列表，检查是否已参加活动 */
export async function cashBackCheck(params: { phone: string }) {
  return request<ResponseReturned>('/Inn/getCashBackInfo', {
    method: 'POST',
    data: params,
  });
}
/** 入库返现列表，更改状态 */
export async function cashBackChangeStatus(params: { status: string; id: string }) {
  return request<ResponseReturned>('/Inn/updateCashBackStatus', {
    method: 'POST',
    data: params,
  });
}
/** 入库返现列表，检查上个月的可发放金额 */
export async function cashBackLastMonthMoney() {
  return request<ResponseReturned>('/Inn/getReleaseInfo', {
    method: 'POST',
  });
}
/** 入库返现列表，导出 */
export async function exportExcel(params: { export: string }) {
  return request<ResponseReturned>('/Inn/exportCashBackList', {
    method: 'POST',
    data: params,
  });
}
